package com.iptv.live.service.impl;

import com.iptv.common.exception.ServiceException;
import com.iptv.common.utils.StringUtils;
import com.iptv.live.domain.LiveTypeTable;
import com.iptv.live.domain.LiveTypeTable2;
import com.iptv.live.domain.dto.LiveTypeTableAddDTO;
import com.iptv.live.domain.dto.LiveTypeTableEditDTO;
import com.iptv.live.domain.vo.LiveTypeTableVO;
import com.iptv.live.mapper.LiveTypeTable2Mapper;
import com.iptv.live.mapper.LiveTypeTableMapper;
import com.iptv.live.service.ILiveTypeTableService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

import static com.iptv.common.constant.HttpStatus.SERVICE_FAIL;

/**
 * 直播频道分类Service业务层处理
 * 
 * @author Cinzia
 * @date 2025-01-03
 */
@Service
public class LiveTypeTableServiceImpl implements ILiveTypeTableService
{
    @Autowired
    private LiveTypeTableMapper liveTypeTableMapper;
    @Resource
    private LiveTypeTable2Mapper liveTypeTable2Mapper;

    private static String password = "123456";

    /**
     * 查询直播频道分类
     * 
     * @param typeId 直播频道分类主键
     * @return 直播频道分类
     */
    @Override
    public LiveTypeTableVO selectLiveTypeTableByTypeId(Long typeId)
    {
        return liveTypeTableMapper.selectLiveTypeTableByTypeId(typeId);
    }

    /**
     * 查询直播频道分类列表
     * 
     * @param liveTypeTable 直播频道分类
     * @return 直播频道分类
     */
    @Override
    public List<LiveTypeTableVO> selectLiveTypeTableList(LiveTypeTable liveTypeTable)
    {
        return liveTypeTableMapper.selectLiveTypeTableList(liveTypeTable);
    }

    /**
     * 新增直播频道分类
     * 
     * @param liveTypeTableAddDTO 直播频道分类
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = ServiceException.class)
    public int insertLiveTypeTable(LiveTypeTableAddDTO liveTypeTableAddDTO)
    {
        int flag = 0;
        try {
            // 检查分类名称是否为空
            if (StringUtils.isEmpty(liveTypeTableAddDTO.getTypeName())) {
                throw new ServiceException("分类名称为空");
            }
            if (liveTypeTableAddDTO.getNeed() == 0) {
                LiveTypeTable liveTypeTable = new LiveTypeTable();
                BeanUtils.copyProperties(liveTypeTableAddDTO, liveTypeTable);
                int result = liveTypeTableMapper.insertLiveTypeTable(liveTypeTable);
                if(result > 0){
                    LiveTypeTable2 liveTypeTable2 = new LiveTypeTable2();
                    liveTypeTable2.setTypeId(liveTypeTable.getTypeId());
                    liveTypeTable2.setNeed(liveTypeTableAddDTO.getNeed());
                    liveTypeTable2.setTypePassword(password);
                    flag = liveTypeTable2Mapper.insertLiveTypeTable2(liveTypeTable2);
                }
            }else {
                LiveTypeTable liveTypeTable = new LiveTypeTable();
                BeanUtils.copyProperties(liveTypeTableAddDTO, liveTypeTable);
                int result = liveTypeTableMapper.insertLiveTypeTable(liveTypeTable);
                if(result > 0){
                    LiveTypeTable2 liveTypeTable2 = new LiveTypeTable2();
                    liveTypeTable2.setTypeId(liveTypeTable.getTypeId());
                    liveTypeTable2.setNeed(liveTypeTableAddDTO.getNeed());
                    liveTypeTable2.setTypePassword(password);
                    flag = liveTypeTable2Mapper.insertLiveTypeTable2(liveTypeTable2);
                }
            }
        } catch (ServiceException e) {
            return new ServiceException("插入直播频道分类失败",SERVICE_FAIL).getCode();
        }
        return flag;
    }

    /**
     * 修改直播频道分类
     * 
     * @param liveTypeTableEditDTO 直播频道分类
     * @return 结果
     */
    @Override
    public int updateLiveTypeTable(LiveTypeTableEditDTO liveTypeTableEditDTO)
    {
        int flag = 0;
        try {
            if (StringUtils.isEmpty(liveTypeTableEditDTO.getTypeName())) {
                throw new ServiceException("分类名称为空");
            }

            LiveTypeTable liveTypeTable = new LiveTypeTable();
            BeanUtils.copyProperties(liveTypeTableEditDTO, liveTypeTable);
            int result = liveTypeTableMapper.updateLiveTypeTable(liveTypeTable);

            if (result > 0) {
                // 根据 need 的值来更新 LiveTypeTable2
                LiveTypeTable2 liveTypeTable2 = new LiveTypeTable2();
                BeanUtils.copyProperties(liveTypeTableEditDTO, liveTypeTable2);

                if (liveTypeTableEditDTO.getNeed() == 0) {
                    // 需要密码，设置密码
                    liveTypeTable2.setTypePassword(password);
                } else {
                    // 不需要密码，清空密码
                    liveTypeTable2.setTypePassword("");
                }

                // 更新 LiveTypeTable2
                flag = liveTypeTable2Mapper.updateLiveTypeTable2(liveTypeTable2);
            }
        } catch (ServiceException e) {
            // 抛出异常而不是返回错误码
            throw e;
        }
        return flag;
    }

    /**
     * 批量删除直播频道分类
     * 
     * @param typeIds 需要删除的直播频道分类主键
     * @return 结果
     */
    @Override
    public int deleteLiveTypeTableByTypeIds(Long[] typeIds)
    {
        return liveTypeTableMapper.deleteLiveTypeTableByTypeIds(typeIds);
    }

    /**
     * 删除直播频道分类信息
     * 
     * @param typeId 直播频道分类主键
     * @return 结果
     */
    @Override
    public int deleteLiveTypeTableByTypeId(Long typeId)
    {
        return liveTypeTableMapper.deleteLiveTypeTableByTypeId(typeId);
    }
}
